package com.cskaoyan.service.impl;

import com.cskaoyan.bean.data.SearchIndexData;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.model.MarketKeyword;
import com.cskaoyan.model.MarketKeywordExample;
import com.cskaoyan.model.MarketSearchHistory;
import com.cskaoyan.model.MarketSearchHistoryExample;
import com.cskaoyan.service.KeyWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class KeyWordServiceImpl implements KeyWordService {
    @Autowired
    MarketKeywordMapper keywordMapper;
    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;

    @Override
    public SearchIndexData queryindex(Integer userId) {
        // 默认第一个关键字对应的信息
        MarketKeywordExample example1 = new MarketKeywordExample();
        example1.createCriteria().andIsDefaultEqualTo(true).andDeletedEqualTo(false);
        MarketKeyword defaultKeyword = keywordMapper.selectByExample(example1).get(0);
        //热门keyword对应的数据
        MarketKeywordExample example = new MarketKeywordExample();
        example.createCriteria().andIsHotEqualTo(true).andDeletedEqualTo(false);
        List<MarketKeyword> hotKeywordList = keywordMapper.selectByExample(example);
        //搜索历史Keyword对应的信息
        MarketSearchHistoryExample example2 = new MarketSearchHistoryExample();
        example2.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(userId);
        example2.setOrderByClause("add_time desc");
        List<MarketSearchHistory> historywordList = searchHistoryMapper.selectByExample(example2);
        List<Map<String,String>> list = new ArrayList<>();
        for (MarketSearchHistory searchHistory : historywordList) {
            Map<String, String> item = new HashMap<>();
            item.put("keyword", searchHistory.getKeyword());
            list.add(item);
        }

        SearchIndexData data = new SearchIndexData();
        data.setDefaultKeyword(defaultKeyword);
        data.setHotKeywordList(hotKeywordList);
        data.setHistoryKeywordList(list);

        return data;
    }
}
